package com.xxh.kfc.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.google.gson.Gson;
import com.xxh.kfc.dao.OrdersDao;
import com.xxh.kfc.entity.Orders;
import com.xxh.kfc.entity.User;
import com.xxh.kfc.entity.msg.ResultMsg;
import com.xxh.kfc.entity.vo.OrderDetailVo;
import com.xxh.kfc.service.OrderDetailService;
import com.xxh.kfc.service.impl.OrderDetailServiceImpl;
import com.xxh.kfc.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

//订单相关操作
@WebServlet(name = "OrderServlet",urlPatterns = "/order/*")
public class OrderServlet extends HttpServlet {
    SqlSession sqlSession=null;
    OrderDetailService orderDetailService=new OrderDetailServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //拿到session
        HttpSession session = request.getSession();

        //拿到sqlsession
        sqlSession = MyBatisUtils.getSqlSession();
        OrdersDao ordersDao = sqlSession.getMapper(OrdersDao.class);
        String pathInfo = request.getPathInfo();

        //拿到用户订单
        if(pathInfo.equals("/getUserOrder")){
            //拿到用户ID
            User userDetail = (User) session.getAttribute("userDetail");
            Integer uid = userDetail.getId();
            List<Orders> orders = ordersDao.getOrderByUid(uid);

            for(int i=0;i<orders.size();i++){
                Date ordertime = orders.get(i).getOrdertime();
                //格式化一下
                String format = DateUtil.formatDate(ordertime);
                DateTime time = DateUtil.parse(format);
                orders.get(i).setOrdertime(time);
            }
            session.setAttribute("orders",orders);
            response.sendRedirect(request.getContextPath()+"/order.jsp");
        }

        //获取该用户所有的订单详情
        if(pathInfo.equals("/getAllOrderDetail")){


            response.setContentType("text/json;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
        }



        //获取该用户某条订单的详情
        if(pathInfo.equals("/getDetail")){

            response.setContentType("text/json;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();

            String oid = request.getParameter("oid");
            List<OrderDetailVo> details = orderDetailService.getDetailsByOid(Integer.parseInt(oid));
            Gson gson = new Gson();

            ResultMsg msgSuccess = new ResultMsg(0, "成功", details.size(), details);
            String json = gson.toJson(msgSuccess);
            out.println(json);
            out.flush();
            out.close();
        }

    }
}
